﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;

namespace BEGDC.CC.Web.Workflow.Common
{
    public class SearchBasePage : BasePage
    {
        private RadGrid GV
        {
            get
            {
                return GetGridView();
            }
        }
        private Button BtnQuery
        {
            get
            {
                return GetQueryBtn();
            }
        }
        protected virtual object GetNewEntity()
        {
            return null;
        }
        protected virtual RadGrid GetGridView()
        {
            return null;
        }
        protected virtual Button GetQueryBtn()
        {
            return null;
        }
        public SearchBasePage()
        {

        }
        protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
        {
            base.RaisePostBackEvent(sourceControl, eventArgument);
            if (sourceControl is RadAjaxManager)
            {
                if (eventArgument == "Rebind")
                {

                }
                else if (eventArgument == "RebindAndNavigate")
                {

                }
                GV.Rebind();
            }
        }

        protected virtual void InitialControl()
        {
            if (GV != null)
            {
                GV.NeedDataSource += new GridNeedDataSourceEventHandler(gv_NeedDataSource);
                GV.ItemCreated += new GridItemEventHandler(GV_ItemCreated);
                GV.ItemCommand += new GridCommandEventHandler(GV_ItemCommand);
                GV.PageIndexChanged += new GridPageChangedEventHandler(GV_PageIndexChanged);
                GV.PageSizeChanged += new GridPageSizeChangedEventHandler(GV_PageSizeChanged);
                GV.SortCommand += new GridSortCommandEventHandler(GV_SortCommand);

            }
            if (BtnQuery != null)
            {
                BtnQuery.Click += new EventHandler(BtnQuery_Click);
            }
        }

        protected virtual void BtnQuery_Click(object sender, EventArgs e)
        {
            GV.Rebind();
        }
        protected virtual void GV_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName.Equals("Delete"))
            {
                object entity = GetNewEntity();
                EntityUtility.FillControlToObject(entity, e.Item);
                DeleteEntity(entity);
                this.GV.Rebind();
            }
        }
        protected virtual void GV_SortCommand(object source, GridSortCommandEventArgs e)
        {
            BindGridData();
        }

        protected virtual void GV_PageSizeChanged(object source, GridPageSizeChangedEventArgs e)
        {
            BindGridData();
        }

        protected virtual void GV_PageIndexChanged(object source, GridPageChangedEventArgs e)
        {
            GV.CurrentPageIndex = e.NewPageIndex;
            BindGridData();
        }

        protected virtual void DeleteEntity(object Entity)
        {
            ;
        }

        protected virtual void GV_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                LinkButton editLink = (LinkButton)e.Item.FindControl("lnkEdit");
                if (editLink != null)
                    editLink.Attributes.Add("OnClick", String.Format("javascript:return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex][e.Item.OwnerTableView.DataKeyNames[0]], e.Item.ItemIndex));
            }
        }
        public virtual void BindGridData()
        {
            if (GV != null)
            {
                this.GV.DataSource = this.GetDataSource();
                GV.DataBind();
            }
        }

        protected virtual void gv_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            this.GV.DataSource = this.GetDataSource();
        }

        protected virtual DataTable GetDataSource()
        {
            return null;
        }

        
    }
}
